package com.evil.mi.service.impl;

import com.evil.mi.dao.VideoImgRepository;
import com.evil.mi.entity.VideoImgEntity;
import com.evil.mi.enums.VideoImgStateEnum;
import com.evil.mi.exception.base.BaseException;
import com.evil.mi.service.VideoImgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * com.evil.mi.service.impl
 *
 * @author 白雨浓
 * date: 2018/8/20 17:06
 * content:
 */
@Service
@Slf4j
public class VideoImgServiceImpl implements VideoImgService {

    @Autowired
    private VideoImgRepository videoImgRepository;


    @Override
    public List<VideoImgEntity> findVideoImgListByVideoId(int videoId) {
        return videoImgRepository.findByVideoId(videoId);
    }

    @Override
    public VideoImgEntity findVideoImgById(int id) {
        return videoImgRepository.findById(id).orElse(null);
    }

    @Override
    public int saveVideoImg(VideoImgEntity entity) {

        entity.setState(VideoImgStateEnum.INIT);
        entity.setCreateTime(new Date());

        entity = videoImgRepository.save(entity);

        log.info("存入图片信息>> imgUrl:{}, videoId:{}", entity.getImgUrl(), entity.getVideoId());
        return entity.getId();
    }

    @Override
    public int modifyVideoImg(VideoImgEntity entity) {

        entity = videoImgRepository.save(entity);

        return entity.getId();
    }

    @Override
    public VideoImgEntity removeVideoImg(int id) {

        VideoImgEntity entity = videoImgRepository.findById(id).orElse(null);

        if (entity == null) {
            throw new BaseException("图片空");
        }

        videoImgRepository.deleteById(id);

        return entity;
    }
}
